#include "dd_circle_2d.h"
#include "math/math_core.h"
#include "graphics/graphics_util.h"
#include "util/mmgr.h"		// Needs to come in early
#include <math.h>

using namespace dd;

Circle2D::Circle2D() :
	m_IsFilled(false)
{
}

Circle2D::~Circle2D()
{

}

void Circle2D::Render(DBG_BUFFER_2D* _pBuffer)
{
	static const int SEGMENTS = 16;
	static const float SegmentMul = (1.0f/(float)SEGMENTS) * MATH_2PI;

	if (m_IsFilled)
	{
		FlushBuffer(_pBuffer);
		// have a way to set solid-color?
		COLOR Color = ColorRGBA( 255, 58, 58, 58);

		for (unsigned int i=0; i<=SEGMENTS; i++)
		{
			float		AngleA = i * SegmentMul;
			float		AngleB = (i+1) * SegmentMul;
			math::Vec2	A(cosf(AngleA)*m_Radius*GraphicsUtil::H2W, sinf(AngleA)*m_Radius );
			math::Vec2	B(cosf(AngleB)*m_Radius*GraphicsUtil::H2W, sinf(AngleB)*m_Radius );
			
			AddVertex(_pBuffer, m_Pos, Color);
			AddVertex(_pBuffer, m_Pos+A, Color);
			AddVertex(_pBuffer, m_Pos+B, Color);

		}
		FlushBufferTri(_pBuffer);
	}

	for (unsigned int i=0; i<=SEGMENTS; i++)
	{
		float		AngleA = i * SegmentMul;
		float		AngleB = (i+1) * SegmentMul;
		math::Vec2	A(cosf(AngleA)*m_Radius*GraphicsUtil::H2W, sinf(AngleA)*m_Radius );
		math::Vec2	B(cosf(AngleB)*m_Radius*GraphicsUtil::H2W, sinf(AngleB)*m_Radius );
		AddLine(_pBuffer, m_Pos+A, m_Pos+B, m_Color); 
	}
}

void Circle2D::Setup(const math::Vec2& _Pos, float Radius, COLOR _Color, bool IsFilled)
{
	m_Color  = _Color;
	m_Pos	 = _Pos;
	m_Radius = Radius;
	m_IsFilled = IsFilled;
}
